import requests
import json

cursor = "2024-10-18T18:09:47.717Z"
counter = 0
# 模拟请求  secret-key 密钥
while True:
    url = "https://www.pexels.com/zh-cn/api/v2/feed"
    params = {'seed': cursor, 'per_page': '12', 'seo_tags': 'true'}
    headers = {
        'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/91.0.4472.124 Safari/537.36',
        'secret-key': 'H2jk9uKnhRmL6WPwh89zBezWvr'
    }
    response = requests.get(url, params=params, headers=headers)
    result = response.content.decode()
    print(response.status_code)
    print(result)
    # 数据解析 json反序列化解析需要的数据字段
    result = json.loads(result)
    cursor = result.get("pagination").get("cursor")
    for item in result.get("data"):
        # print(item)
        alt = item.get("attributes").get("alt")
        download_link = item.get("attributes").get("image")
        if not download_link:
            # 非照片类资源
            pass
        else:
            counter += 1
            download_link = download_link.get("download_link")
            print(counter, alt, download_link)
            # with open(f'./pexels/{alt}.jpg','wb') as f:
            #     f.write(requests.get(download_link).content)
